import { Request } from "../../utils/request.js"
var request = new Request()
const app = getApp()
Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    //弹出登录授权页面显示开关
    loginModal: false,
    //用户面板信息
    createTime : '0',
    studyTime : '0',
    effectiveTime : '0',
    effective : '0',
  },
  //判断是否获取用户信息
  onLoad: function () {
    if (app.globalData.userInfo) {
      let that = this;
      that.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      });
    }
  },
  onShow() {
    this.getTopData();
  },
  authNav(e) {
    console.log(e.currentTarget.dataset.url)
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          wx.navigateTo({
            url: e.currentTarget.dataset.url,
          })
        } else {
          wx.showModal({
            title: '警告',
            content: '未授权登录，无法使用功能',
            showCancel: false,
            confirmText: '确定',
          })
        }
      }
    })
  },
  getUserInfo(e) {
    if (e.detail.userInfo) {  //用户按了允许授权按钮
      //获取用户信息并登录
      this.toLogin();
    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '警告',
        content: '您点击了拒绝授权，将无法进入小程序',
        showCancel: false,
        confirmText: '返回授权',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击了“返回授权”')
          }
        }
      })
    }
  },
  toLogin() {
    var that = this
    wx.getUserInfo({
      lang: "zh_CN",
      success: res => {
        var resultData = res
        var openId = wx.getStorageSync('openId') || ''
        if (openId == '') {
          wx.login({
            success: res => {
              if (res.code) {
                console.log("登录")
                request.post("wx/login", {
                  code: res.code, //临时登录凭证
                  rawData: resultData.rawData, //用户非敏感信息
                  signature: resultData.signature, //签名
                  encrypteData: resultData.encryptedData, //用户敏感信息
                  iv: resultData.iv //解密算法的向量
                }, function (res) {
                  console.log(res);
                  if (res.statusCode == 200) {
                    console.log("成功")
                    app.globalData.openid = res.data.result.openid;
                    wx.setStorageSync('openId', res.data.result.openid)

                    app.globalData.userInfo = resultData.userInfo
                    wx.setStorageSync('userInfo', resultData.userInfo)
                    that.setData({
                      userInfo: resultData.userInfo,
                      hasUserInfo: true
                    })

                    that.getTopData();
                    that.hideLoginModal()
                  }
                })
              }
            }
          });
        }


        if (this.userInfoReadyCallback) {
          this.userInfoReadyCallback(res)
        }
      }
    })
  },
  getTopData() {
    var that = this
    var openId = wx.getStorageSync('openId')
    request.get("totaltime/" + openId, null, function (res) {
      console.log(res)
      if (res.statusCode == 200) {
        var data = res.data.result
        var st = parseInt(data.time)
        var et = parseInt(data.effectiveTime)
        var ef = data.effective
        that.setData({
          createTime: data.createTime,
          studyTime: data.time,
          effectiveTime: data.effectiveTime,
          effective: ef
        })
      }
    })
  },
  //展示登录授权页面
  showLoginModal: function (e) {
    this.setData({
      loginModal: true
    })
  },
  //登录授权页面
  hideLoginModal: function (e) {
    this.setData({
      loginModal: false
    })
  }
})
